package models.repository;

import models.entities.ShoppingCartEntity;
import models.entities.ShoppingCartItem;
import models.exceptions.CreateEntityException;
import models.exceptions.DeleteEntityException;
import models.exceptions.GetEntityException;
import models.exceptions.UpdateEntityException;
import models.helpers.DBSession;
import models.repository.common.ShoppingCartRepository;
import mybatis.mappers.ShoppingCartMapper;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;

@Deprecated
public class ShoppingCartRepositorySQL implements ShoppingCartRepository {
    public void addProduct(int id, int userId) throws CreateEntityException, IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            ShoppingCartMapper shoppingCartMapper = dbSession.getMapper(ShoppingCartMapper.class);
            shoppingCartMapper.addProduct(id, userId, 0);
            dbSession.commit();
        } catch (Exception ex) {
            throw new CreateEntityException(MessageFormat.format("Cannot add product to shopping cart: '{0}' and userId: {1}; Original message: {2}", id, userId, ex.getMessage()));
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public ShoppingCartEntity getShoppingCart(int userId) throws GetEntityException, IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            ShoppingCartMapper shoppingCartMapper = dbSession.getMapper(ShoppingCartMapper.class);
            List<ShoppingCartItem> shoppingItems = shoppingCartMapper.getItems(userId);
            return new ShoppingCartEntity(shoppingItems);
        } catch (Exception ex) {
            throw new GetEntityException(MessageFormat.format("Cannot get shopping cart for user: '{0}'; Original message: {1}", userId, ex.getMessage()));
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void delete(int productId, int userId) throws DeleteEntityException, IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            ShoppingCartMapper shoppingCartMapper = dbSession.getMapper(ShoppingCartMapper.class);
            shoppingCartMapper.delete(productId, userId);
            dbSession.commit();
        } catch (Exception ex) {
            throw new DeleteEntityException(MessageFormat.format("Cannot delete product from cart: '{0}' and userId: {1}; Original message: {2}", productId, userId, ex.getMessage()));
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }

    public void update(ShoppingCartEntity shoppingCart, int userId) throws UpdateEntityException, IOException {
        SqlSession dbSession = DBSession.openSession();
        try {
            ShoppingCartMapper shoppingCartMapper = dbSession.getMapper(ShoppingCartMapper.class);

            for (ShoppingCartItem shoppingItem : shoppingCart.items) {
                shoppingCartMapper.delete(shoppingItem.productId, userId);
                if (shoppingItem.quantity > 0) {
                    shoppingCartMapper.addProduct(shoppingItem.productId, userId, shoppingItem.quantity - 1);
                }
            }
            dbSession.commit();
        } catch (Exception ex) {
            throw new UpdateEntityException(MessageFormat.format("Cannot update shopping cart; Original message: {0}", ex.getMessage()));
        } finally {
            if (dbSession != null) {
                dbSession.close();
            }
        }
    }
}
